<?php
define("TOKEN", "FlyingWings");
class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature())
		{
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){
            /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
               the best way is to check the validity of xml by yourself */
            libxml_disable_entity_loader(true);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $messageType = trim($postObj->MsgType);//自定义变量，存储消息类型

            switch($messageType)
            {
                case "text":
                    $resultStr = $this->handleText($postObj);
                    break;
                case "event":
                    $resultStr = $this->handleEvent($postObj);
                    break;
                /*		case "location":
                $resultStr = $this->handleLocal($postObj);*/
                default:
                    $resultStr = "Unknow msg type: ".$messageType;
                    break;
            }

            echo $resultStr;


        }else {
            echo "";
            exit;
        }
    }

    private function checkSignature()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }

        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function  handleText($postObj)//对文字信息调用该函数
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";
        if(!empty( $keyword ))
        {

            $msgType = "text";
            switch(trim($keyword))
            {
                /*case '列表':
                    $contentStr = "Welcome to wechat world!";break;*/
                case 'h':
                    $contentStr =$fromUsername;break;
                
                case '1':
                
				
				$contentStr="111";

				break;
				
				case '2':
				$contentStr="222";break;
				case '3':
				$contentStr="333";
				break;
				
                default:
                    $contentStr="欢迎来到及时伞！请输入\n【1】绑定平台账号 \n【2】借伞 \n【3】还伞 \n";break;

            }
            if(strstr($keyword,"表"))
            {
                $contentStr="<a href='http://www.baidu.com'>欢迎来到百度列表</a>";
            }


            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            return $resultStr;
        }

    }
	public function handleEvent($postObj)
{
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
		$msgType="text";
		$time = time();
		$event="CLICK";
		$eventkey="";
		$textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";
		if($postObj->Event="CLICK")
		{
		switch($postObj->EventKey)
		{
			case "0":
			$contentStr=$fromUsername;
			
			
			break;
			
			case "2":
			$contentStr="顾客还伞";
			break;
			case "3":
			include_once("./controller/getAllUser.php");
			$a =new GetAllUser($fromUsername);
			$contentStr=$a->getName().$a->getType();
			break;
			case "4":
			$contentStr="租客租伞";
			include_once('./model/umbrella.php');
			$b = new Umbrella("123456");
			$contentStr=$b->getID();
			break;
			case "5":
			$contentStr="租客收伞";
			break;
			case "6":
			$contentStr="租客绑定";
			break;
			default:
                    $contentStr="欢迎来到及时伞！请输入\n【1】绑定平台账号 \n【2】借伞 \n【3】还伞 \n";break;
		}
		}
		
		 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,$contentStr);
		return $resultStr;
}

}
?>
